<?php
/**
 * 每日统计当月迟到的信息，月底统计发送一份全部迟到的信息通知HR
 *@author coldarmy
 * */
apf_require_class('Oa_Job_Attendance_Abstract');
class Oa_Job_Attendance_LateInform extends Oa_Job_Attendance_Abstract {

    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    public function run($params){
        $this->log("开始检测...");
        /** 每月15日及月末跑一次数据 **/
        if(date('d') == 16 || date('d') == date('t')) {
            $this->deal_with_late_info();
        }
        $this->log("检测完毕");
    }

    /**
     * 检查数据如当月有两次以上迟到的信息，邮件通知
     *
     * @access private
     * @return void
     */
    private function deal_with_late_info() {
        $start_time = mktime(0,0,0,date('m'),1,date('Y'));
        $start = 0;
        $row = 300;
        $now = time();
        $have = array();
        while(true) {
            $sql = "SELECT user_id,start_work,access_date FROM access_day_record WHERE created > $start_time limit $start,$row";
            $back = $this->get_db_rows($sql, $this->readdb, array());
            if(empty($back)) {
                break;
            }
            $start += $row;
            foreach ($back as $key => $val) {
                if($val['start_work'] >= '931') {
                    $have[$val['user_id']][] = $val;
                }
            }
        }
        $late = $this->check_leave_data($have);
    }

    /**
     * 检查请假记录中，是否为请假中
     *
     * @param array $have
     * @param date $date
     * @access private
     * @return empty or int
     */
    private function check_leave_data($have) {
        $back = false;
        if(empty($have)) {
            return $back;
        }
        $all = array();
        foreach ($have as $key => $val) {
            $id = $key;
            $q_sql = "SELECT apply_time,start_time,end_time FROM leave_record WHERE user_id = $id";
            $tmp = $this->get_db_rows($q_sql, $this->readdb, array());
            $num = 0;
            $each_user = array();
            foreach ($val as $k => $v) {
                $apply = false;
                if(empty($tmp)) {
                    $num++;
                    $each_user[$id] = $num;
                    continue;
                }
                foreach ($tmp as $tk => $tv) {
                    if(empty($v['access_date'])) {
                        continue;
                    }
                    $ac = $v['access_date'];
                    $latetime = mktime(9,31,0,substr($ac,5,6),substr($ac,7,8),substr($ac,0,4));
                    $start = strtotime($tv['start_time']);
                    $end = strtotime($tv['end_time']);
                    if($latetime >= $start && $latetime <= $end) {
                        $apply = true;
                        break;
                    }
                }
                if(!$apply) {
                    $num++;
                    $each_user[$id] = $num;
                }

            }
            if($num >= 2) {
                $this->send_mail_inform_user($id);
            }
        }

        $lastday = date('t');
        if($each_user && date('d') == $lastday) {
            $this->send_mail_inform_hr($each_user);
        }
        return $back;
    }

    /**
     * 对于超过两次迟到的员工，邮件通知该员工
     *
     * @param mixed $id
     * @access private
     * @return void
     */
    private function send_mail_inform_user($id) {
        if(empty($id) || !is_numeric($id)) {
            return '';
        }
        $user = $this->get_user_info_by_id($id);
        if(empty($user)) {
            return '';
        }
        $content = "Hi ".$user['name'].":\n   本周你已迟到两次或以上,请准时上班哦！";
        //Tools_SendMail::get_instance()->send($user['email'],'迟到两次友情通知！',$content);
        apf_require_class('Tools_SendMail');
        Tools_SendMail::get_instance()->send($user['email'],'迟到两次友情通知！',$content);
    }

    private function send_mail_inform_hr($all) {
        if(empty($all)) {
            return '';
        }
        $send = array();
        foreach ($all as $key => $val) {
            $id = $key;
            $user = $this->get_user_info_by_id($id);
            $s['user_name'] = $user[0]['name'];
            $s['user_code'] = $user[0]['code'];
            $s['department'] = $user[0]['city_name'].$user[0]['department_name'];
            $s['num'] = $val;
            $send[] = $s;
        }
        $mailcontent = "本月迟到人员信息统计：<br><table border='1' cellpadding='0' cellspacing='0'>
                        <tr><td>姓名</td><td>员工号</td><td>部门</td><td>迟到次数</td></tr>";
        foreach ($send as $key => $val) {
            $mailcontent .= "<tr><td>{$val['user_name']}</td><td>{$val['user_code']}</td><td>{$val['department']}</td><td>{$val['num']}</td></tr>";
        }
        $mailcontent .= "</table>";
        //echo $mailcontent;
        apf_require_class('Tools_SendMail');
        Tools_SendMail::get_instance()->send('fionayu@anjukeinc.com',date('Y-m').'月份迟到员工信息汇总！',$mailcontent);
    }


    /**
     * 根据ID获取用户信息
     *
     * @param array|int $ids
     * @access public
     * @return array()
     */
    public function get_user_info_by_id($ids) {
        $result = array();
        if(empty($ids)) {
            return $result;
        }
        if(is_array($ids)) {
            $sql = "SELECT * FROM user_info WHERE user_id IN ($ids)";
        } else {
            $sql = "SELECT * FROM user_info WHERE user_id = $ids";
        }
        $result = $this->get_db_rows($sql, $this->readdb, array());
        return $result;
    }
}

